package admin_controller

import (
	"cloud-navigation/app/common/request"
	"cloud-navigation/app/common/response"
	"cloud-navigation/app/models"
	"cloud-navigation/app/services"
	"cloud-navigation/global"
	"fmt"
	"strconv"

	"github.com/gin-gonic/gin"
)

// 导航列表资源控制器
type Navigation struct{}

// 获取所有导航
func (navigation Navigation) Index(c *gin.Context) {
	var (
		navi  []models.Navigation
		total int64
	)
	page, limit, validator := request.GetPages(c)
	name, url, assort, validator := request.GetNavQuery(c)
	if validator.Message != "" {
		response.ValidateFail(c, validator.Message, validator)
		return
	}
	db := global.App.DB.Table("navigations")
	if name != "" {
		db = db.Where("name like ?", "%"+name+"%")
	}
	if url != "" {
		db = db.Where("url like ?", "%"+url+"%")
	}
	if assort != 0 {
		db = db.Where("assort_id = ?", assort)
	}
	db.Count(&total)
	db = db.Preload("Assort").Order("created_at, sort").Offset(int((page - 1) * limit)).Limit(int(limit))
	err := db.Find(&navi).Error
	if err != nil {
		response.BusinessFail(c, "查询失败")
		return
	}
	response.Success(c, response.PageResponse[models.Navigation]{
		Total:       total,
		CurrentPage: page,
		Data:        navi,
	})
}

// 新建导航
func (navigation Navigation) Create(c *gin.Context) {
	var (
		navi  request.Navigation
		model models.Navigation
	)
	if !services.Request(c, &navi) {
		return
	}
	model = models.Navigation{
		AssortID: navi.AssortID,
		Desc:     navi.Desc,
		Icon:     navi.Icon,
		Url:      navi.Url,
		Name:     navi.Name,
		Sort:     uint8(navi.Sort)}
	err := global.App.DB.Create(&model).Error
	if err != nil {
		fmt.Println(err)
		response.BusinessFail(c, "创建失败")
		return
	}
	services.WriteLogs(c, "添加导航: name="+navi.Name, 0)
	response.Success(c, &model)
}

// 更新导航
func (navigation Navigation) Update(c *gin.Context) {
	var (
		uri   request.Uri
		navi  request.Navigation
		model models.Navigation
	)
	if err := c.ShouldBindUri(&uri); err != nil {
		response.BusinessFail(c, "参数错误")
		return
	}
	if !services.Request(c, &navi) {
		return
	}
	model.ID.ID = uri.Id
	err := global.App.DB.Model(&model).Updates(navi).Error
	if err != nil {
		response.BusinessFail(c, "更新失败")
		return
	}
	services.WriteLogs(c, "更新导航: name="+navi.Name, 0)
	response.Success(c, &model)
}

// 删除导航
func (navigation Navigation) Delete(c *gin.Context) {
	var (
		uri request.Uri
	)
	if err := c.ShouldBindUri(&uri); err != nil {
		response.BusinessFail(c, "参数错误")
		return
	}
	if err := global.App.DB.Delete(&models.Navigation{}, uri.Id).Error; err != nil {
		response.BusinessFail(c, "删除失败")
		return
	}
	services.WriteLogs(c, "删除导航: id="+strconv.FormatUint(uint64(uri.Id), 10), 0)
	response.Success(c, nil)
}

// 导航详情
func (navigation Navigation) Show(c *gin.Context) {
	var (
		uri  request.Uri
		navi models.Navigation
	)
	if err := c.ShouldBindUri(&uri); err != nil {
		response.BusinessFail(c, "参数错误")
		return
	}
	if err := global.App.DB.First(&navi, uri.Id).Error; err != nil {
		response.BusinessFail(c, "查询失败")
		return
	}
	response.Success(c, navi)
}
